package com.rraj.common.web.controller;


import com.rraj.common.constants.Constants;
import com.rraj.common.domain.ResultBody;
import com.rraj.common.enums.PlatformEnums;
import com.rraj.common.enums.ResCodeEnums;
import com.rraj.common.utils.StrUtils;
import com.rraj.common.web.RequestHeaderEnums;
import com.rraj.common.web.UserSessionData;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.BindingResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 
 * @author xiaohai
 * @date:  2017年3月7日 下午4:59:07   
 *
 */
public abstract class BaseController {

	/**
	 * 验证请求参数
	 * 
	 * @param result
	 * @param bind
	 * @return
	 */
	public ResultBody validateResult(ResultBody result, BindingResult bind) {
		if (bind.hasErrors()) {
			result.setResMsg(bind.getAllErrors().get(0).getDefaultMessage());
			result.setResCode(ResCodeEnums.ERROR.getResCode());
		}
		return result;
	}

	/**
	 * 用户
	 * 
	 * @param request
	 * @return
	 */
	public UserSessionData getUserToken(HttpServletRequest request) {
		return (UserSessionData) request.getAttribute(Constants.USER_DATA);
	}

	/**
	 * Id
	 * @param request
	 * @return
	 */
	public Long getMemberId(HttpServletRequest request) {
		UserSessionData user = getUserToken(request);
		if (user != null) {
			return user.getId();
		}
		return 0L;
	}

	/**
	 * 手机号
	 * 
	 * @param request
	 * @return
	 */
	public String getUserMobile(HttpServletRequest request) {
		UserSessionData user = getUserToken(request);
		if (user != null) {
			return user.getMobile();
		}
		return null;
	}

	/**
	 * 获取登录设备
	 * 
	 * @param req
	 * @return
	 */
	protected String getRequestDevice(HttpServletRequest req) {
		return req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE.getHeader());
	}

	/**
	 * 获取登录设备类型
	 * 
	 * @param req
	 * @return
	 */
	protected int getRequestClientSource(HttpServletRequest req) {
		String deviceType = req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE_TYPE.getHeader());
		if (StringUtils.isNotBlank(deviceType) && StringUtils.isNumeric(deviceType)) {
			return Integer.valueOf(deviceType);
		}
		return -1;
	}

	/**
	 * 获取设备ID
	 * @param req
	 * @return
	 */
	protected String getRequestDeviceId(HttpServletRequest req) {
		String deviceId = req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE_ID.getHeader());
		if (StringUtils.isNotBlank(deviceId)) {
			return deviceId;
		}
		return null;
	}

	/**
	 * 获得推送编号
	 * @param req
	 * @return
	 */
	protected String getRequestPushId(HttpServletRequest req) {
		return req.getHeader(RequestHeaderEnums.REQ_HEADER_PUSH_ID.getHeader());
	}
	
	/**
	 * 渠道
	 * @param req
	 * @return
	 */
	protected String getRequestChannelSource(HttpServletRequest req) {
		return req.getHeader(RequestHeaderEnums.REQ_HEADER_CHANNEL_SOURCE.getHeader());
	}
	
	/**
	 * 获取客户端IP
	 * @Title: getRequestClientIP   
	 * @Description: TODO
	 * @param: @param request
	 * @param: @return      
	 * @return: String      
	 * @throws
	 */
	public String getRequestClientIP(HttpServletRequest request) {
		int deviceType = getRequestClientSource(request);
		if (deviceType == PlatformEnums.H5.getValue()) {// h5过来的界面ip需要特殊处理
			String ip = request.getParameter("ip");
			if (StringUtils.isNoneBlank(ip)) {
				return ip;
			}
			ip = request.getHeader("ip");
			if (StringUtils.isNotEmpty(ip)) {
				return ip;
			}
		}
		String ip = request.getHeader("x-forwarded-for");
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getHeader("WL-Proxy-Client-IP");
		}
		if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
			ip = request.getRemoteAddr();
		}
		if (StringUtils.isNotBlank(ip) && ip.indexOf(",") > 0) {
			ip = ip.substring(0, ip.indexOf(","));
		}
		return ip;
	}
	
	/**
	 * 
	 * @param req
	 * @return
	 */
	public boolean isWebPay(HttpServletRequest req){
		return false;
	}
	
	/**
	 * 是否IOS设备
	 * 
	 * @param req
	 * @return
	 */
	public boolean isIOS(HttpServletRequest req) {
		int deviceType =  getRequestClientSource(req);
		if (deviceType == PlatformEnums.IOS.getValue()) {
			return true;
		}
		return false;
	}
	
	/**
	 * 获取设备序列
	 * 
	 * @param req
	 * @return
	 */
	protected String getRequestDeviceImei(HttpServletRequest req) {
		String deviceImei = req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE_ID.getHeader());
		if (StrUtils.isNotBlank(deviceImei)) {
			return deviceImei;
		}
		return null;
	}
	
	/**
	 * 获取登录设备类型
	 * 
	 * @param req
	 * @return
	 */
	protected Short getRequestDeviceType(HttpServletRequest req) {
		String deviceType = req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE_TYPE.getHeader());
		if (StrUtils.isNotBlank(deviceType)
				&& StrUtils.isNumeric(deviceType)) {
			return Short.valueOf(deviceType);
		}
		return -1;
	}
	
	/**
	 * 获取设备序列
	 * 
	 * @param req
	 * @return
	 */
	protected String getRequestDeviceSerialNumber(HttpServletRequest req) {
		return req.getHeader(RequestHeaderEnums.REQ_HEADER_DEVICE_ID.getHeader());
	}
}
